#!/bin/bash

# 运行多个游戏自动化实例的脚本

show_help() {
    echo "用法: $0 [选项]"
    echo ""
    echo "选项:"
    echo "  -p, --port PORT     指定VNC端口 (默认: 5900)"
    echo "  -n, --name NAME     指定实例名称 (默认: default)"
    echo "  -h, --help          显示此帮助信息"
    echo ""
    echo "示例:"
    echo "  $0 -p 5901 -n instance1    # 运行实例1，VNC端口5901"
    echo "  $0 -p 5902 -n instance2    # 运行实例2，VNC端口5902"
    echo ""
    echo "多实例运行:"
    echo "  $0 -p 5900 -n game1"
    echo "  $0 -p 5901 -n game2"
    echo "  $0 -p 5902 -n game3"
}

# 默认值
VNC_PORT=5901
INSTANCE_NAME="default"

# 解析命令行参数
while [[ $# -gt 0 ]]; do
    case $1 in
        -p|--port)
            VNC_PORT="$2"
            shift 2
            ;;
        -n|--name)
            INSTANCE_NAME="$2"
            shift 2
            ;;
        -h|--help)
            show_help
            exit 0
            ;;
        *)
            echo "未知选项: $1"
            show_help
            exit 1
            ;;
    esac
done

# 检查端口是否被占用
if lsof -Pi :$VNC_PORT -sTCP:LISTEN -t >/dev/null ; then
    echo "❌ 错误: 端口 $VNC_PORT 已被占用"
    echo "请选择其他端口或停止占用该端口的服务"
    exit 1
fi

echo "🚀 启动游戏自动化实例..."
echo "📊 配置信息:"
echo "   - 实例名称: $INSTANCE_NAME"
echo "   - VNC端口: $VNC_PORT"
echo "   - 容器名称: vjoyxy-automation-$INSTANCE_NAME"

# 创建实例专用目录结构
INSTANCE_DIR="/Users/jinsimin/Documents/vjoyxy/$INSTANCE_NAME"
mkdir -p "$INSTANCE_DIR/games"
mkdir -p "$INSTANCE_DIR/screenshots" 
mkdir -p "$INSTANCE_DIR/logs"
mkdir -p "$INSTANCE_DIR/config"
mkdir -p "$INSTANCE_DIR/scripts"

# 导出环境变量
export VNC_PORT=$VNC_PORT
export VNC_HOST_PORT=$VNC_PORT
export COMPOSE_PROJECT_NAME="vjoyxy-$INSTANCE_NAME"

# 创建临时的docker-compose文件
cat > "docker-compose-$INSTANCE_NAME.yml" <<EOF
services:
  game-automation:
    image: vjoyxy-game-automation
    container_name: vjoyxy-automation-$INSTANCE_NAME
    ports:
      - "$VNC_PORT:$VNC_PORT"
    volumes:
      - $INSTANCE_DIR/games:/app/games:rw
      - $INSTANCE_DIR/screenshots:/app/screenshots:rw
      - $INSTANCE_DIR/logs:/app/logs:rw
      - $INSTANCE_DIR/config:/app/config:rw
      - $INSTANCE_DIR/scripts:/app/scripts:rw
    environment:
      - DISPLAY=:99
      - VNC_PORT=$VNC_PORT
      - WINEPREFIX=/app/.wine
      - WINEARCH=win32
      - JAVA_OPTS=-Xmx2g -Xms512m
    restart: unless-stopped
    shm_size: 2gb
    stdin_open: true
    tty: true
    security_opt:
      - seccomp:unconfined
    networks:
      - automation-network-$INSTANCE_NAME

networks:
  automation-network-$INSTANCE_NAME:
    driver: bridge
EOF

echo "🚀 启动容器..."
docker compose -f "docker-compose-$INSTANCE_NAME.yml" up -d

if [ $? -eq 0 ]; then
    echo "✅ 实例启动成功!"
    echo ""
    echo "🔌 连接信息:"
    echo "   - VNC地址: localhost:$VNC_PORT"
    echo "   - 实例目录: /Users/jinsimin/Documents/vjoyxy/$INSTANCE_NAME/"
    echo "   - 游戏目录: /Users/jinsimin/Documents/vjoyxy/$INSTANCE_NAME/games/"
    echo "   - 截图目录: /Users/jinsimin/Documents/vjoyxy/$INSTANCE_NAME/screenshots/"
    echo "   - 日志目录: /Users/jinsimin/Documents/vjoyxy/$INSTANCE_NAME/logs/"
    echo ""
    echo "📋 管理命令:"
    echo "   查看日志: docker compose -f docker-compose-$INSTANCE_NAME.yml logs -f"
    echo "   停止实例: docker compose -f docker-compose-$INSTANCE_NAME.yml down"
    echo "   进入容器: docker compose -f docker-compose-$INSTANCE_NAME.yml exec game-automation bash"
else
    echo "❌ 启动失败!"
    exit 1
fi 